package pe.edu.upc.e41a.javasoft.domain;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;
import pe.edu.upc.e41a.javasoft.service.TransaccionService;

@Configurable
@Component
@RooDataOnDemand(entity = Transaccion.class)
public class TransaccionDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<Transaccion> data;

	@Autowired
    private PublicacionDataOnDemand publicacionDataOnDemand;

	@Autowired
    private UsuarioDataOnDemand usuarioDataOnDemand;

	@Autowired
    TransaccionService transaccionService;

	public Transaccion getNewTransientTransaccion(int index) {
        Transaccion obj = new Transaccion();
        setCantidad(obj, index);
        setComentario(obj, index);
        setFechaRegistro(obj, index);
        setPublicacion(obj, index);
        setUsuario(obj, index);
        return obj;
    }

	public void setCantidad(Transaccion obj, int index) {
        Integer cantidad = new Integer(index);
        obj.setCantidad(cantidad);
    }

	public void setComentario(Transaccion obj, int index) {
        String comentario = "comentario_" + index;
        if (comentario.length() > 100) {
            comentario = comentario.substring(0, 100);
        }
        obj.setComentario(comentario);
    }

	public void setFechaRegistro(Transaccion obj, int index) {
        Date fechaRegistro = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setFechaRegistro(fechaRegistro);
    }

	public void setPublicacion(Transaccion obj, int index) {
        Publicacion publicacion = publicacionDataOnDemand.getRandomPublicacion();
        obj.setPublicacion(publicacion);
    }

	public void setUsuario(Transaccion obj, int index) {
        Usuario usuario = usuarioDataOnDemand.getRandomUsuario();
        obj.setUsuario(usuario);
    }

	public Transaccion getSpecificTransaccion(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        Transaccion obj = data.get(index);
        Integer id = obj.getCodigo();
        return transaccionService.findTransaccion(id);
    }

	public Transaccion getRandomTransaccion() {
        init();
        Transaccion obj = data.get(rnd.nextInt(data.size()));
        Integer id = obj.getCodigo();
        return transaccionService.findTransaccion(id);
    }

	public boolean modifyTransaccion(Transaccion obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = transaccionService.findTransaccionEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'Transaccion' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<Transaccion>();
        for (int i = 0; i < 10; i++) {
            Transaccion obj = getNewTransientTransaccion(i);
            try {
                transaccionService.saveTransaccion(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
